import http from "@/http";
import { DevicePricingPlan, DevicePricingQuery, DevicePricingCreateDTO, DevicePricingUpdateDTO, DevicePricingBatchCopyParam } from './type';

// 分页查询设备核价列表
export const getDevicePricingListApi = (params: DevicePricingQuery) => {
  return http.get("/devicePricing/queryDevicePricingList", params);
};

// 根据ID获取设备核价详情
export const getDevicePricingDetailApi = (id: number | string) => {
  return http.get(`/devicePricing/getDevicePricingDetail/${id}`);
};

// 新增设备核价
export const addDevicePricingApi = (data: DevicePricingCreateDTO) => {
  return http.post("/devicePricing/addDevicePricing", data);
};

// 更新设备核价
export const updateDevicePricingApi = (data: DevicePricingUpdateDTO) => {
  return http.put("/devicePricing/updateDevicePricing", data);
};

// 删除设备核价
export const deleteDevicePricingApi = (id: number | string) => {
  return http.delete(`/devicePricing/deleteDevicePricing/${id}`);
};

// 获取堆垛机价格数据
export const getStackerPriceDataApi = () => {
  return http.post("/devicePricing/calculateStacker");
};

// 更新单个数值
export const updateDefaultValueApi = (data: any) => {
  return http.put("/devicePricing/updateDefaultValue", data);
};

/**
 * 计算堆垛机价格
 * @param data 堆垛机数据
 */
export function calculateStackerPricingApi(data: any) {
  return http.post("/devicePricing/calculateStacker", data);
}

// 更新设备核价状态
export const updateStatusApi = (params: { id: number; activate: number }) => {
  return http.put("/devicePricing/updateStatus", params);
};

// 导入设备核价
export const importDevicePricingApi = (file: File) => {
  const formData = new FormData();
  formData.append('file', file);
  return http.upload("/devicePricing/import", formData);
};

// 导出设备核价
export const exportDevicePricingApi = (params?: DevicePricingQuery) => {
  // 构建查询字符串
  const queryString = params ? new URLSearchParams(
    Object.entries(params).reduce((acc, [key, value]) => {
      if (value !== undefined && value !== null && value !== '') {
        acc[key] = String(value);
      }
      return acc;
    }, {} as Record<string, string>)
  ).toString() : '';
  
  const url = `${http['instance'].defaults.baseURL}/devicePricing/export${queryString ? '?' + queryString : ''}`;
  window.location.href = url;
  return Promise.resolve();
};

// 自定义字段导出设备核价
export const customExportDevicePricingApi = (params: { 
  exportFields: string[];
  queryParams?: DevicePricingQuery;
}) => {
  return http.post("/devicePricing/customExport", params, {
    responseType: 'blob'
  });
};

// 下载设备核价导入模板
export const downloadDevicePricingTemplateApi = () => {
  const url = `${http['instance'].defaults.baseURL}/devicePricing/downloadTemplate`;
  window.location.href = url;
  return Promise.resolve();
};

// 批量更新设备核价状态
export const batchUpdateStatusApi = (params: { ids: number[], activate: number }) => {
  return http.put("/devicePricing/batchUpdateStatus", params);
};

// 批量复制设备核价
export const batchCopyApi = (data: DevicePricingBatchCopyParam) => {
  return http.post("/devicePricing/batchCopy", data);
}; 